<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="Docker搭建Redis集群"><meta name="keywords" content="Redis,Docker,集群"><meta name="author" content="qqggzwm"><meta name="copyright" content="qqggzwm"><title>Docker搭建Redis集群 | qqggzwmの博客</title><link rel="shortcut icon" href="/blog/melody-favicon.ico"><link rel="stylesheet" href="/blog/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.3.0'
} </script><meta name="generator" content="Hexo 5.3.0"><link rel="alternate" href="/blog/atom.xml" title="qqggzwmの博客" type="application/atom+xml">
</head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://gitee.com/qqggzwm/blog-img/raw/master/img/20210120225331.jpg"></div><div class="author-info__name text-center">qqggzwm</div><div class="author-info__description text-center"></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/blog/archives"><span class="pull-left">文章</span><span class="pull-right">5</span></a><a class="author-info-articles__tags article-meta" href="/blog/tags"><span class="pull-left">标签</span><span class="pull-right">5</span></a><a class="author-info-articles__categories article-meta" href="/blog/categories"><span class="pull-left">分类</span><span class="pull-right">3</span></a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/blog/">qqggzwmの博客</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/blog/archives">Archives</a><a class="site-page" href="/blog/tags">Tags</a><a class="site-page" href="/blog/categories">Categories</a></span><span class="pull-right"></span></div><div id="post-info"><div id="post-title">Docker搭建Redis集群</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2021-03-18</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/blog/categories/Redis/">Redis</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>首先要了解redis的集群概念，这里不再赘述</p>
<p>首先，我们启用三个不同的端口作为从机</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker run -d -p 6379:6379 --name redis01 redis</span><br><span class="line">docker run -d -p 6380:6379 ----name redis02 redis</span><br><span class="line">docker run -d -p 6381:6379 -name redis03 redis</span><br></pre></td></tr></table></figure>
<p>6382 作为主机</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -d -p 6382:6379 --name redismaster redis</span><br></pre></td></tr></table></figure>


<p>查看镜像运行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker ps</span><br></pre></td></tr></table></figure>
<p><img src="https://gitee.com/wx_9aca2fe07f/blog-img/raw/master/img/20210318230651.png" alt="image-20210318230651756"></p>
<p>查看主机的元数据查看容器内网地址（IPAddress：172.17.0.6），后面需要用到 ip 地址来绑定主机</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker inspect redismaster # 要把主机的地址记下</span><br></pre></td></tr></table></figure>
<p><img src="https://gitee.com/wx_9aca2fe07f/blog-img/raw/master/img/20210318203830.png" alt="image-20210318203823019 其中一个从机的地址"></p>
<p>​                                                    </p>
<p>分别进入从机 redis01、 redis02 、redis03 内部</p>
<p>执行命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost ~]# docker exec -it redis01 &#x2F;bin&#x2F;bash # 以交互模式进入</span><br><span class="line">root@f90aa020be57:&#x2F;data# redis-cli #启动redis客户端</span><br><span class="line">127.0.0.1:6379&gt; slaveOf 172.17.06 6379 #认定ip 端口为主机</span><br><span class="line">OK</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; info replication # 查看主从信息</span><br><span class="line"># Replication</span><br><span class="line">role:slave # 当前为从节点</span><br><span class="line">master_host:172.17.06 # 主节点ip</span><br><span class="line">master_port:6379 # 主节点端口</span><br><span class="line">master_link_status:up</span><br><span class="line">master_last_io_seconds_ago:3</span><br><span class="line">master_sync_in_progress:0</span><br><span class="line">slave_repl_offset:28</span><br><span class="line">slave_priority:100</span><br><span class="line">slave_read_only:1</span><br><span class="line">connected_slaves:2</span><br><span class="line">slave0:ip&#x3D;172.17.0.4,port&#x3D;6379,state&#x3D;online,offset&#x3D;28,lag&#x3D;0</span><br><span class="line">slave1:ip&#x3D;172.17.0.5,port&#x3D;6379,state&#x3D;online,offset&#x3D;28,lag&#x3D;0</span><br><span class="line">master_failover_state:no-failover</span><br><span class="line">master_replid:27a0077a51642d85be1932a30a93f95f9dfa21f5</span><br><span class="line">master_replid2:0000000000000000000000000000000000000000</span><br><span class="line">master_repl_offset:28</span><br><span class="line">second_repl_offset:-1</span><br><span class="line">repl_backlog_active:1</span><br><span class="line">repl_backlog_size:1048576</span><br><span class="line">repl_backlog_first_byte_offset:1</span><br><span class="line">repl_backlog_histlen:28</span><br></pre></td></tr></table></figure>
<p>这样我们就配置好了一个从节点，重复以上命令把从机2和从机3也配置了</p>
<p>我们在主节点redismaster添加数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">set k1 v1</span><br></pre></td></tr></table></figure>
<p><img src="https://gitee.com/wx_9aca2fe07f/blog-img/raw/master/img/20210318230849.png" alt="image-20210318230849078"></p>
<p>然后在从节点redis01中查看</p>
<p><img src="https://gitee.com/wx_9aca2fe07f/blog-img/raw/master/img/20210318230932.png" alt="image-20210318230931946"></p>
<p>可以查看到 k1，但是要注意从节点只能读不能写（即 set 和其他相关的操作）</p>
<blockquote>
<p>注意:以上使用的是命令的方式设置主从节点，一旦主节点宕机（关机），从节点将变为主节点。</p>
<p>采用配置文件的方式可以保证主从关系的稳定性，在这里不再做演示，只需要在开始时容器启动的时候指定配置文件启动即可</p>
</blockquote>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">qqggzwm</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://qqggzwm.gitee.io/blog/posts/920b4d45/">http://qqggzwm.gitee.io/blog/posts/920b4d45/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://qqggzwm.gitee.io/blog">qqggzwmの博客</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/Redis/">Redis</a><a class="post-meta__tags" href="/blog/tags/Docker/">Docker</a><a class="post-meta__tags" href="/blog/tags/%E9%9B%86%E7%BE%A4/">集群</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/blog/posts/7b223055/"><i class="fa fa-chevron-left">  </i><span>Mysql 内连接，外连接傻傻分不清楚？一文介绍清楚</span></a></div><div class="next-post pull-right"><a href="/blog/posts/f7ede91d/"><span>Java基础</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2019 - 2021 By qqggzwm</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/blog/js/utils.js?version=1.9.0"></script><script src="/blog/js/fancybox.js?version=1.9.0"></script><script src="/blog/js/sidebar.js?version=1.9.0"></script><script src="/blog/js/copy.js?version=1.9.0"></script><script src="/blog/js/fireworks.js?version=1.9.0"></script><script src="/blog/js/transition.js?version=1.9.0"></script><script src="/blog/js/scroll.js?version=1.9.0"></script><script src="/blog/js/head.js?version=1.9.0"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script></body></html>